﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;


//Click bài viết ==> set Session["cate"]


public partial class Controls_Template_TreeView : System.Web.UI.UserControl
{   
    class_database db;
    class_treeview treeView;

    public string rawURL = HttpContext.Current.Request.ServerVariables["URL"];
    public string idMauTemp = "0";

    protected void Page_Load(object sender, EventArgs e)
    {
        if ((string)Session["username"] == null || (string)Session["username"] == "")
        {
            Response.Redirect("Login.aspx");
        }
        if (Request.QueryString["idMau"] != null)
        {
            string idMau = Convert.ToString(class_string.clear_inject(Request.QueryString["idMau"].Trim()));
            if (idMau != "0")
            {
                idMauTemp = idMau;
            }
        }
        if (!IsPostBack)
        {
            FillTree();
        }
    }




    protected void FillTree()
    {
        treeView = new class_treeview();
        DataTable dt = treeView.getTreeData();
        treeView1.Nodes.Clear();
        if (dt != null && dt.Rows.Count > 0)
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                TreeNode tnLoaiMau = new TreeNode();
                tnLoaiMau.Text = dt.Rows[i]["name"].ToString();
                tnLoaiMau.Value = dt.Rows[i]["id"].ToString();
                string value = dt.Rows[i]["id"].ToString();
                tnLoaiMau.Collapse();
                string url = rawURL + "?module=Template&view=General&idLoaiMau=" + dt.Rows[i]["id"].ToString();
                tnLoaiMau.NavigateUrl = url;
                treeView1.Nodes.Add(tnLoaiMau);
                FillChild(tnLoaiMau, value);
            }
        }
    }


    public void FillChild(TreeNode parent, string parentID)
    {
        db = new class_database();
        treeView = new class_treeview();
        DataTable dt = treeView.getTreeNodeData(parentID);
        if (dt != null && dt.Rows.Count > 0)
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                TreeNode tnMau = new TreeNode();
                tnMau.Text = dt.Rows[i]["name"].ToString();
                tnMau.Value = dt.Rows[i]["id"].ToString();
                string value = dt.Rows[i]["id"].ToString();
                tnMau.Collapse();
                string url = rawURL + "?module=Template&view=General&idMau=" + value;
                tnMau.NavigateUrl = url;
                parent.ChildNodes.Add(tnMau);

                string sql = "SELECT tbl_loaibaiphantich.name, tbl_baiphantich.id FROM tbl_baiphantich, tbl_loaibaiphantich"
                    + " WHERE tbl_baiphantich.idloaibai = tbl_loaibaiphantich.id AND tbl_baiphantich.idmau = " + value;
                DataTable dt2 = db.GetDataTable(sql);
                if (dt2 != null && dt2.Rows.Count > 0)
                {
                    for (int j = 0; j < dt2.Rows.Count; j++)
                    {
                        TreeNode tnBaiViet = new TreeNode();
                        tnBaiViet.Text = dt2.Rows[j]["name"].ToString();
                        string text = tnBaiViet.Text;
                        tnBaiViet.Value = dt2.Rows[j]["id"].ToString();
                        tnBaiViet.Expand();
                        if (tnBaiViet.Text != "Mẫu + Pdb + Hàm Diệt + Data")
                        {
                            string stringCate = "";
                            if (text == "Thông Tin Chung")
                            {
                                stringCate = "ThongTinChung";
                            }
                            else if (text == "Thông Tin Kỹ Thuật Chi Tiết")
                            {
                                stringCate = "ThongTinKyThuatChiTiet";
                            }
                            else if (text == "Thông Tin File")
                            {
                                stringCate = "ThongTinFile";
                            }
                            else if (text == "Thông Tin Hàm Diệt")
                            {
                                stringCate = "ThongTinHamDiet";
                            }
                            Session["nameBV"] = stringCate;
                            string urlBaiViet = rawURL + "?module=Template&view=General&nameBV=" + stringCate + "&idMau=" + tnMau.Value;
                            tnBaiViet.NavigateUrl = urlBaiViet;
                            
                        }
                        tnMau.ChildNodes.Add(tnBaiViet);
                    }
                }
            }
        }
    }


    public void GetTreeNodeFromName(string keySearch)
    {
        string sql = "SELECT id, name FROM tbl_mau WHERE name LIKE '%" + keySearch + "%'";
        db = new class_database();
        DataTable dt = db.GetDataTable(sql);
        if (dt != null && dt.Rows.Count > 0)
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                TreeNode node = treeView1.FindNode(dt.Rows[i]["name"].ToString());
                node.Expand();                
            }
        }
    }

}